<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Exceptions\HttpResponseException;

class AjaxPacketRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'game_id' => 'required',
            'game_name' => 'required',
            'channel_id' => 'required',
            'channel_name' => 'required',
            'sub_channel_id' => 'required',
            'sub_channel_name' => 'required',
            'server_id' => 'required',
            'server_name' => 'required',

            'uid' => 'required',//用户UID
            'account' => 'required',//游戏账号
            'nickname' => 'required',//游戏昵称

            'no' => 'required',//订单号
            'to_account' => 'required',//收款账号
            'real_name' => 'required',//真实姓名
            'money' => 'required',
            'url' => 'required',//回调地址
            'sign' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'game_id.required' => 'game_id不能为空',
            'game_name.required' => 'game_name不能为空',
            'channel_id.required' => 'channel_id不能为空',
            'channel_name.required' => 'channel_name不能为空',
            'sub_channel_id.required' => 'sub_channel_id不能为空',
            'sub_channel_name.required' => 'sub_channel_name不能为空',
            'server_id.required' => 'server_id不能为空',
            'server_name.required' => 'server_name不能为空',

            'uid.required' => 'uid不能为空',
            'account.required' => 'account不能为空',
            'nickname.required' => 'nickname不能为空',

            'no.required' => 'no不能为空',
            'to_account.required' => 'to_account不能为空',
            'real_name.required' => 'real_name不能为空',
            'money.required' => 'money不能为空',
            'url.required' => 'url不能为空',
            'sign.required' => 'sign不能为空',
        ];
    }

    protected function prepareForValidation()
    {
        $reqExt = isset($this->ext) ? $this->ext : '';

        $this->merge([
            'ext' => $reqExt
        ]);
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        $error = implode('|', $error);

        throw new HttpResponseException(
            response()->json(['code' => -1, 'msg' => $error], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
        );
    }

    protected function getValidatorInstance()
    {
        return parent::getValidatorInstance()->after([$this, "afterValidation"]);
    }

    public function afterValidation()
    {
        //验证sign
        $config = config('config.red_packet');
        $params = $this->all();
        ksort($params);
        unset($params['sign']);
        $pwd = implode('', $params);

        $sign_read = hash_pbkdf2("sha256", $pwd, $config['salt'], 1000, 20);
        if ($this['sign'] != $sign_read) {
            throw new HttpResponseException(
                response()->json(['code' => -1, 'msg' => 'sign错误'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
            );
        }
    }
}
